import axios from "axios";
import router from "../router";
import { Loading, Message } from "element-ui";
import qs from "qs";
const instance = axios.create({
  timeout: 150000,
  //baseURL: 'http://shixunmis.zjtongji.edu.cn/'
  baseURL: process.env.VUE_APP_BASEAPI
  //baseURL: 'http://4703f226.nat123.fun:33359'
  //baseURL: 'http://127.0.0.1:9012/'
  //baseURL: 'http://shixunmis.zjtongji.edu.cn/server/'
  //baseURL: 'http://192.168.1.10:9012/'
});
// request拦截器，实现loading加载
let loadingInstance = "";
// 添加请求拦截器，在请求头中加token
instance.interceptors.request.use(
  config => {
    //config.headers['Access-Control-Allow-Origin'] = '*';
    //config.headers['Access-Control-Allow-Credentials'] = 'true';
    //config.headers['Access-Control-Allow-Headers'] = 'content-type';
    //config.headers['Access-Control-Request-Method'] = 'GET,POST';
    loadingInstance = Loading.service({});
    if (config.url.indexOf("noFilter/login") == -1) {
      config.headers['token'] = JSON.parse(localStorage.getItem('userMsg')).token;
      config.headers['ticket'] = JSON.parse(localStorage.getItem('userMsg')).ticket;
    }
    if (config.url.indexOf("/v1/captcha") != -1 || config.url.indexOf("noFilter/login") != -1){
      return config;
    } else {
      /*router.push("/login");
      Message({
        message: "token已过期,请重新登陆!",
        type: "error",
        duration: 5 * 1000
      });*/
    }
    loadingInstance = Loading.service({});
    return config;
  },
  error => {
    loadingInstance.close();
    Message({
      message: error.message,
      type: "error",
      duration: 5 * 1000
    });
    return Promise.reject(error);
  }
);
// response拦截器，实现loading关闭
instance.interceptors.response.use(
  data => {
    loadingInstance.close();
    return data;
  },
  error => {
    Message({
      message: error.message,
      type: "error",
      duration: 5 * 1000
    });
    loadingInstance.close();
    return Promise.reject(error);
  }
);

export default {
  get(url, param) {
    //get请求
    return new Promise(resolve => {
      instance({
        method: "get",
        url,
        params: param
      }).then(res => {
        resolve(res);
      });
    });
  },
  post(url, param) {
    // if(this.GLOBAL.ticketValue == ''){
    //   this.$router.push("/login");
    //   return ;
    // }
    // url = url + "?" + this.GLOBAL.ticket + "=" + this.GLOBAL.ticketValue;
    // console.log(url);
    //post请求
    return new Promise(resolve => {
      instance({
        method: "post",
        url,
        data: qs.stringify(param),
        headers: { "X-Requested-With": " XMLHttpRequest " }
      }).then(res => {
        if(res.data.status === -1) {
          router.push("/login");
          Message({
            message: "token已过期,请重新登陆!",
            type: "error",
            duration: 5 * 1000
          })
          return;
        }
        resolve(res);
      });
    });
  },
  put(url, param) {
    //post请求
    return new Promise(resolve => {
      instance({
        method: "put",
        url,
        data: qs.stringify(param),
        headers: { "X-Requested-With": " XMLHttpRequest " }
      }).then(res => {
        resolve(res);
      });
    });
  }
};
